package com.aldobucchi.x.flash.geom
{
	import flash.geom.Point;
	
	import mx.utils.StringUtil;

	public class PolarPoint extends Point
	{
		
		
		private var _len:Number;		
		
		public function get len( ):Number
		{
			return _len;
		}
		
		
		
		
		private var _angle:Number;
		
		public function get angle( ):Number
		{
			return _angle;
		}
		
		
		
		
		/**
		 * 
		 * @param len
		 * @param angle
		 * 			in Radians
		 * 
		 */		
		public function PolarPoint( len:Number, angle:Number )
		{
			
			var p:Point = Point.polar( len, angle );
			
			super( p.x, p.y );
			
			_len = len;
			_angle = angle;
			
		}
		
		
		override public function toString():String
		{
			return StringUtil.substitute( 
				"({0},{1})[ r={2}, rads:{3} PI, deg:{4} ]", 
				x, y, len, angle/Math.PI, GeomUtil.radiansToDegrees( angle )
				)		
		}
		
			
		
		override public function clone( ):Point
		{
			return new PolarPoint( len, angle );	
		}
		
		
		
		
		public static function fromPoint( point:Point ):PolarPoint
		{
		
			if ( point is PolarPoint )
			{
				return ( point as PolarPoint ).clone( ) as PolarPoint; 
			}
		
			var len:Number = Point.distance( PointUtil.ORIGIN, point );
			var angle:Number = Math.atan2( point.y, point.x );
			
			return new PolarPoint( len, angle );
		
		}
		
	}
	
}